package com.example.thecharacterbook;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    private EditText search_InternetLine ;
    private ImageButton search_InternetBtn ;
    private WebView web = null ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internet_search);
        search_InternetBtn= findViewById(R.id.search_InternetBtn);
        search_InternetLine=findViewById(R.id.search_InternetLine);
        web =findViewById(R.id.searchFrom);
        search_InternetBtn.setOnClickListener(new View.OnClickListener() {
            String strURL;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String str_word =search_InternetLine.getText().toString();
                if(str_word!=null){
                    str_word =str_word.trim(); //去除一下空格
                    strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q="
                            + str_word; //组成请求的url
                    //加载网页
                    web.loadUrl(strURL);
                    web.setWebViewClient(new WebViewClient(){
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view,
                                                                String url) {
                            // TODO Auto-generated method stub
                            view.loadUrl(url);//不跳到系统浏览器
                            return true;
                        }
                    });
                    //进度条
                    web.setWebChromeClient(new WebChromeClient(){
                        public void onProgressChanged(WebView view, int newProgress) {

                            MainActivity2.this.setProgress(newProgress*100);//设置进度条
                        };
                    });

                }else {
                    Toast.makeText(MainActivity2.this,"单词不能为空!" ,Toast.LENGTH_SHORT).show();
                }


            }
        });
    }


}